using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace The_LarX
{
    public static class Script
    {
        public static char SeparadorTraco = "-".ToCharArray()[0];
        public static char SeparadorDoisPontos = ":".ToCharArray()[0];

        public static void Run(List<string> Script)
        {
            for (int i = 0; i < Script.Count; i++)
            {
                RunScript(Script[i]);
            }
        }
        public static object RunScript(string Script)
        {
            string finalScript = Script;

            if (Script.StartsWith("script:"))
            {
                finalScript = Script.Remove(0, 7);
            }

            string[] Codigo = finalScript.ToLower().Split(SeparadorTraco);
            string[] CaseCodigo = finalScript.Split(SeparadorTraco);

            try
            {
                #region Variaveis
                bool Add = false;
                bool Change = false;
                bool Get = false;
                #endregion

                #region Checar
                if (Codigo[0].StartsWith("add") || Codigo[0].StartsWith("criar"))
                {
                    Add = true;
                }
                if (Codigo[0].StartsWith("change") || Codigo[0].StartsWith("mudar"))
                {
                    Change = true;
                }
                if (Codigo[0].StartsWith("get"))
                {
                    Get = true;
                }
                if (Codigo[0].StartsWith("exit"))
                {
                    if (Engine.Game != null)
                    {
                        Engine.Game.Exit();
                    }
                }
                #endregion

                #region Add/Criar
                if (Add)
                {                    
                    Assembly a = Assembly.Load(CaseCodigo[1]);
                    Type Type = a.GetType(CaseCodigo[2]);
                    object Object = System.Activator.CreateInstance(Type);

                    #region GameScreen
                    if (Object is GameScreen)
                    {
                        GameScreen Screen = (GameScreen)Object;
                        for (int i = 0; i < Codigo.Length; i++)
                        {
                            if (Codigo[i].ToLower().StartsWith("name:"))
                            {
                                string res = Codigo[i].Remove(0, 5);
                                Screen.Name = res;
                            }
                        }
                    }
                    #endregion

                    #region Component
                    if (Object is Component)
                    {
                        Component Comp = (Component)Object;
                        for (int i = 0; i < Codigo.Count<string>(); i++)
                        {
                            if (Codigo[i].StartsWith("screen:"))
                            {
                                string res = Codigo[i].Remove(0, 7);
                                Engine.GameScreens.GetScreen(res).AddComponent(Comp);
                            }
                            if (Codigo[i].StartsWith("name:"))
                            {
                                string res = Codigo[i].Remove(0, 5);
                                Comp.Name = res;
                            }
                        }
                    }
                    #endregion
                }
                #endregion

                #region Change/Mudar
                if (Change)
                {
                    if (Codigo[1].ToLower().StartsWith("screen:"))
                    {
                        string[] res = Codigo[1].Split(SeparadorDoisPontos);
                        GameScreen Screen = Engine.GameScreens.GetScreen(res[1]);
                        Type Type = Screen.GetType();
                        if (Codigo[2].StartsWith("bool"))
                        {
                            if (Codigo[4].StartsWith("true"))
                            {
                                Type.GetField(Codigo[3]).SetValue(Screen, true);
                            }
                            if (Codigo[4].StartsWith("false"))
                            {
                                Type.GetField(Codigo[3]).SetValue(Screen, false);
                            }
                        }
                        else if (Codigo[2].StartsWith("int"))
                        {
                            Type.GetField(Codigo[3]).SetValue(Screen, int.Parse(Codigo[3].ToString()));
                        }
                        else if (Codigo[2].StartsWith("float"))
                        {
                            Type.GetField(Codigo[3]).SetValue(Screen, float.Parse(Codigo[3].ToString()));
                        }
                        else if (Codigo[2].StartsWith("string"))
                        {
                            Type.GetField(Codigo[3]).SetValue(Screen, Codigo[4]);
                        }
                    }
                    if (Codigo[1].ToLower().StartsWith("component:"))
                    {
                        string[] rescomp = Codigo[1].Split(SeparadorDoisPontos);
                        string[] resscreen = Codigo[2].Split(SeparadorDoisPontos);
                        GameScreen Screen = Engine.GameScreens.GetScreen(resscreen[1]);
                        Component Comp = Screen.GetComponent(rescomp[1]);
                        Type Type = Screen.GetType();
                        if (Codigo[3].StartsWith("bool"))
                        {
                            if (Codigo[5].StartsWith("true"))
                            {
                                Type.GetField(Codigo[4]).SetValue(Screen, true);
                            }
                            if (Codigo[5].StartsWith("false"))
                            {
                                Type.GetField(Codigo[4]).SetValue(Screen, false);
                            }
                        }
                        else if (Codigo[3].StartsWith("int"))
                        {
                            Type.GetField(Codigo[4]).SetValue(Screen, int.Parse(Codigo[5].ToString()));
                        }
                        else if (Codigo[3].StartsWith("float"))
                        {
                            Type.GetField(Codigo[4]).SetValue(Screen, float.Parse(Codigo[5].ToString()));
                        }
                        else if (Codigo[3].StartsWith("string"))
                        {
                            Type.GetField(Codigo[4]).SetValue(Screen, Codigo[5]);
                        }
                    }
                    if (Codigo[1].ToLower().StartsWith("sprite:"))
                    {
                        string[] res = Codigo[1].Split(SeparadorDoisPontos);
                        Sprite2D Sprite = null;
                        for (int i = 0; i < EngineSystem.Sprites.Count; i++)
                        {
                            if (EngineSystem.Sprites[i].Name == res[1])
                            {
                                Sprite = EngineSystem.Sprites[i];
                            }
                        }
                        if (Sprite == null)
                            return null;

                        Type Type = Sprite.GetType();

                        if (Codigo[2].StartsWith("bool"))
                        {
                            if (Codigo[4].StartsWith("true"))
                            {
                                Type.GetProperty(Codigo[3]).SetValue(Sprite, true, null);                                
                            }
                            if (Codigo[4].StartsWith("false"))
                            {
                                Type.GetProperty(Codigo[3]).SetValue(Sprite, false, null);                                
                            }
                        }
                        else if (Codigo[2].StartsWith("int"))
                        {
                            Type.GetProperty(Codigo[3]).SetValue(Sprite, int.Parse(Codigo[4].ToString()), null);
                        }
                        else if (Codigo[2].StartsWith("float"))
                        {
                            Type.GetProperty(Codigo[3]).SetValue(Sprite, float.Parse(Codigo[4].ToString()), null);
                        }
                        else if (Codigo[2].StartsWith("string"))
                        {
                            Type.GetProperty(Codigo[3]).SetValue(Sprite, Codigo[4].ToString(), null);
                        }
                    }
                }
                #endregion

                #region Get
                if (Get)
                {
                    if (Codigo[1].StartsWith("language"))
                    {
                        return LanguageManager.GetString(int.Parse(CaseCodigo[2]));
                    }
                }
                #endregion
            }
            catch
            {
            }

            return null;
        }
    }
}
